import Vue from 'vue' //引入vue
import VueRouter from 'vue-router' //引入vue-router

import {
    checkToken
} from "@/apis/users" //验证token是否过期
Vue.use(VueRouter) //注册插件
import Login from '../views/Login.vue' //登录
const router = new VueRouter({
    routes: [{
            path: '/', //登录界面
            name: 'login',
            component: Login
        },
        {
            path: '/home', //主页
            name: 'home',
            component: () => import('../views/Home.vue'), //懒加载
            children: [{
                    path: '/home/homepages', //后台首页
                    name: 'homepages',
                    component: () => import('../views/homemodules/Homepages.vue')
                }, {
                    path: '/home/homeorders', //订单管理
                    name: 'homeorders',
                    component: () => import('../views/homemodules/Homeorders.vue')
                }, {
                    path: '/home/Commodityman', //商品管理
                    name: 'Commodityman',
                    component: () => import('../views/homemodules/Commodityman.vue'),

                },
                {
                    path: '/home/commodityclas', //商品分类
                    name: 'commodityclas',
                    component: () => import('../views/merchandisemodules/Commodityclas.vue'),
                }, {
                    path: '/home/addproduct', //添加商品
                    name: 'addproduct',
                    component: () => import('../views/merchandisemodules/Addproduct.vue'),
                }, {
                    path: '/home/productList', //商品列表
                    name: 'productList',
                    component: () => import('../views/merchandisemodules/ProductList.vue'),
                }, {
                    path: '/home/Storeman', //店铺管理
                    name: 'storeman',
                    component: () => import('../views/homemodules/Storeman.vue')

                }, {
                    path: '/home/accountman', //账号管理
                    name: 'accountman',
                    component: () => import('../views/homemodules/Accountman.vue'),
                },
                {
                    path: '/home/accountlist', //账号列表
                    name: 'accountlist',
                    component: () => import('../views/accountmoudles/Accountlist.vue'),
                },
                {
                    path: '/home/addaccount', //添加账号
                    name: 'addaccount',
                    component: () => import('../views/accountmoudles/Addaccount.vue'),
                },
                {
                    path: '/home/editpassword', //修改密码
                    name: 'editpassword',
                    component: () => import('../views/accountmoudles/Editpassword.vue'),
                }, {
                    path: '/home/salestatistics', //销售统计>商品统计
                    name: 'salestatistics',
                    component: () => import('../views/homemodules/Salestatistics.vue')
                },
                {
                    path: '/home/orderstatistics', //订单统计
                    name: 'orderstatistics',
                    component: () => import('../views/homemodules/Orderstatistics.vue')
                },
                {
                    path: '/home/manger', //管理员信息
                    name: 'manger',
                    component: () => import('../views/homemodules/Manger.vue')
                },
            ]
        },
        {
            path: '*',
            component: () => import( '../views/Error404') 
        }
    ]

})
//路由跳转的生命周期
router.beforeEach((to, from, next) => {
    // console.log(to, from, next);
    if (to.path != "/") {
        //访问除登录以外的其他页面开的token验证
        checkToken({
            token: localStorage.token
        }).then((res) => {

            if (res.data.code == 0) {
                next() //如果token有效,直接允许访问对应的页面
            } else {
                next('/')
            } //否则直接跳转到首页
        })
    } else {
        next()
    }
})
export default router